package com.project.controller.web;

import com.project.entity.Image;
import com.project.myBeans.ResponseBean;
import com.project.service.ImagesService;
import com.project.service.base.BaseService;
import com.project.service.base.CrpData.CryFileUtil;
import com.project.utils.Base64Util;
import com.project.utils.Util;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.util.Arrays;

/**
 * Created by WheelChen on 2017/6/21.
 */
@Controller
@RequestMapping("/image")
public class ImageController extends BaseService {
    @Autowired
    ImagesService imagesService;

    @Autowired
    private CryFileUtil cryFileUtil;

    final static int ENCRY = 1; //需要加密

    /**
     * author: WheelChen
     * date: 2017-6-21
     * function: 新建图片信息
     *
     * @param image
     * @return 图片id
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResponseBean add(@RequestBody Image image) {
        ResponseBean responseBean = new ResponseBean();
        try {
            imagesService.add(image);
            responseBean.setData(image.getId());
            responseBean.setSuccess(true);
            return responseBean;
        } catch (Exception e) {
            responseBean.setMessage("文件存储失败");
            responseBean.setSuccess(false);
            return responseBean;
        }
    }

    @RequestMapping(value = "/find/{id}", method = RequestMethod.GET)
    @ResponseBody
    /**
     * author: WheelChen
     * date: 2017/7/19
     * function: 根据id查询字符串
     *
     * @param id
     * @return
     */
    public ResponseBean find(@PathVariable("id") Integer id, HttpServletRequest request) {
        ResponseBean responseBean = new ResponseBean();
        try {
            Image image = imagesService.findById(id);
            if (ENCRY == image.getIsencrypted()) {
                String absolutePath = request.getSession().getServletContext().getRealPath(image.getPath());
                String base64 = imagesService.dec(absolutePath);//base64编码的图片数据
                responseBean.setData(base64);
            }
            else {
                responseBean.setData(image.getPath());
            }

//            String ext = "data:image/" + Util.getExt(image.getPath()) + ";"; // 文件格式
//
//            System.out.println(absolutePath);
//
//            File f = new File(absolutePath);
//
//            FileInputStream fis =new FileInputStream(f);
//
//            byte[] all = new byte[(int) f.length()];
//
//            fis.read(all);
//
//            String base64 = Base64Util.encode(cryFileUtil.decry(savePath));
//
//            String fullpath = ext + "base64," + base64;
            responseBean.setSuccess(true);
        } catch (Exception e) {
            responseBean.setMessage("程序异常");
            responseBean.setSuccess(false);
        }
        return responseBean;
    }

}
